package com.shaoshaohuo.app.ui.ec;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.MyQuoteDetailEntity;
import com.shaoshaohuo.app.entity.QuoteDetail;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper.Field;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.view.TopbarView;

public class MyQuoteDetailActivity extends BaseActivity implements OnClickListener {

	private TopbarView mTopbarView;

	private TextView mPriceText;
	private TextView mSupplyNameText;
	private TextView mContactNameText;
	private TextView mContactPhoneText;

	private Button mNextButton;

	private String supplyuserid;
	private String orderid;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(new TopbarView(this));
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				setContentView(R.layout.activity_quote_detail);
				initData();
				initView();
				setUpView();
				reqeustData();
			}
		}, 200);
	}

	protected void initData() {
		Intent intent = getIntent();
		supplyuserid = SharedPreferencesHelper.getString(Field.USER_ID, "");
		orderid = intent.getStringExtra(ExtraName.orderid);
	}

	protected void reqeustData() {
		startLoadingDialog();
		RequestService.getInstance().getMyQuoteDetail(this, supplyuserid, orderid, MyQuoteDetailEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
				    MyQuoteDetailEntity entity = (MyQuoteDetailEntity) resultData;
					updateUi(entity);
				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {

			}
		});
	}

	protected void updateUi(MyQuoteDetailEntity entity) {
		QuoteDetail data = entity.getData().getQuotedata();
		mPriceText.setText("￥"+data.getPrice()+"元");
		mSupplyNameText.setText("供应商名称："+data.getSuppliers());
		mContactNameText.setText("联系人："+data.getRealname());
		mContactPhoneText.setText("联系电话："+data.getPhone());
		
		if("0".equals(data.getStatus())){
			mNextButton.setVisibility(View.VISIBLE);
		}else{
			mNextButton.setVisibility(View.GONE);
		}
	}

	
	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mNextButton = (Button) this.findViewById(R.id.button_next);
		
		mPriceText = (TextView) this.findViewById(R.id.textview_price);
		mSupplyNameText = (TextView) this.findViewById(R.id.textview_supply_name);
		mContactNameText = (TextView) this.findViewById(R.id.textview_contact_name);
		mContactPhoneText = (TextView) this.findViewById(R.id.textview_contact_phone);
	}

	private void setUpView() {
		mTopbarView.setCenterText("我的报价");
		mTopbarView.setLeftView(true, true);
//		mTopbarView.setRightText("取消报价");
//		mTopbarView.setRightClickListener(new OnClickListener() {
//            
//            @Override
//            public void onClick(View v) {
//                cancelQuote();
//            }
//        });
		mNextButton.setText("退出报价");
		mNextButton.setOnClickListener(this);
		mNextButton.setVisibility(View.GONE);
	}

	protected void cancelQuote() {
	    startLoadingDialog();
	    RequestService.getInstance().cancelSupplyOrder(this, orderid, BaseEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    showToast("退出报价成功");
//                    mTopbarView.setRightText("");
//                    mNextButton.setVisibility(View.GONE);
                    finish();
                }else{
                    showToast(resultData.getMsg());
                }
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    @Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_next:
			showConfimDialog();
			break;
		}
	}

	private void showConfimDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确认退出报价?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
//				selectSupplier();
			    cancelQuote();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}

////	protected void selectSupplier() {
////		startLoadingDialog();
////		RequestService.getInstance().selectQuote(this, id, orderid, BaseEntity.class, new RequestListener() {
////			
////			@Override
////			public void onSuccess(int requestCode, BaseEntity resultData) {
////				dismissLoadingDialog();
////				if(resultData.isOk()){
////					showToast("选择供应商成功");
//////					reqeustData();
////					toOrderDetail();
////				}else{
////					showToast(resultData.getMsg());
////				}
////			}
////			
////			@Override
////			public void onFailed(int requestCode, Exception error, String msg) {
////				dismissLoadingDialog();
////				showToast(R.string.please_check_network);
////			}
////		});
////		
////		Intent intent = new Intent(this, PurchaseOrderDetailActivity.class);
////		startActivity(intent);
////	}
//
//    protected void toOrderDetail() {
//        Intent intent = new Intent(this, PurchaseOrderDetailActivity.class);
//        intent.putExtra(ExtraName.orderid, orderid);
//        startActivity(intent);
//    }

}
